(function() {

this.codium = this.codium || {};

this.codium.App = pasta.webgui.App.extend(
{
	init : function() 
	{
		this._super();
		
		this.logged = false;
	},	

	run : function() 
	{
		this._super();
		
		stage.setBackground(this.libDir + "/img/wallpaper/simpleblue.jpg", "center top");
		
		this.service = new codium.CodiumService();
		
		this.menu = new codium.panel.Menu();
		this.menu.setLeft(30);
		this.menu.setTop(0);
		this.menu.setHeight(stage.getHeight());
		this.menu.getOnSelect().setHandler(this, this.menuSelect);
		this.menu.setVisible(false);
		stage.addControl(this.menu);
		
		this.userInfo = new codium.panel.UserInfo();
		this.userInfo.setLeft(stage.getWidth() - this.userInfo.getWidth() - 10);
		this.userInfo.setTop(5);
		this.userInfo.setVisible(false);
		stage.addControl(this.userInfo);
		
		this.overview = new codium.panel.Overview();
		this.overview.setLeft(this.menu.getLeft() + this.menu.getWidth() + 10);
		this.overview.setTop(70);
		this.overview.setWidth(stage.getWidth() - this.overview.getLeft() - 10);
		this.overview.setHeight(stage.getHeight() - this.overview.getTop() - 10);
		this.overview.tag = "overview";
		this.overview.setVisible(false);
		stage.addControl(this.overview);
		
		this.milestone = new codium.panel.Milestone();
		this.milestone.setLeft(this.overview.getLeft());
		this.milestone.setTop(this.overview.getTop());
		this.milestone.setWidth(this.overview.getWidth());
		this.milestone.setHeight(this.overview.getHeight());
		this.milestone.tag = "milestone";
		this.milestone.setVisible(false);
		stage.addControl(this.milestone);
		
		this.ticket = new codium.panel.Ticket();
		this.ticket.setLeft(this.overview.getLeft());
		this.ticket.setTop(this.overview.getTop());
		this.ticket.setWidth(this.overview.getWidth());
		this.ticket.setHeight(this.overview.getHeight());
		this.ticket.tag = "ticket";
		this.ticket.setVisible(false);
		stage.addControl(this.ticket);
		
		this.file = new codium.panel.File();
		this.file.setLeft(this.overview.getLeft());
		this.file.setTop(this.overview.getTop());
		this.file.setWidth(this.overview.getWidth());
		this.file.setHeight(this.overview.getHeight());
		this.file.tag = "files";
		this.file.setVisible(false);
		stage.addControl(this.file);
		
		this.people = new codium.panel.People();
		this.people.setLeft(this.overview.getLeft());
		this.people.setTop(this.overview.getTop());
		this.people.setWidth(this.overview.getWidth());
		this.people.setHeight(this.overview.getHeight());
		this.people.tag = "people";
		this.people.setVisible(false);
		stage.addControl(this.people);
		
		this.setting = new codium.panel.Setting();
		this.setting.setLeft(this.overview.getLeft());
		this.setting.setTop(this.overview.getTop());
		this.setting.setWidth(this.overview.getWidth());
		this.setting.setHeight(this.overview.getHeight());
		this.setting.tag = "setting";
		this.setting.setVisible(false);
		stage.addControl(this.setting);
		
		stage.addResizeListener(this);
		
		this.login = new codium.panel.Login();
		this.login.setLeft((stage.getWidth() - this.login.getWidth()) / 2);
		this.login.setTop((stage.getHeight() - this.login.getHeight()) / 2);
		this.login.getOnLogin().setHandler(this, this.loginLogin);
		stage.addControl(this.login);
		
		this.activePanel = null;
	},
	
	menuSelect : function(sender, id)
	{
		if (id == "exit")
		{
			window.location = "index.php";
		}
		else
		{
			var targetPanel = null;
			
			for (var i = 0; i < stage.controls.getLength(); i++)
			{
				var panel = stage.controls.get(i);
				
				if (panel.tag == id)
				{
					targetPanel = panel;
					break;
				}
			}
			
			if (targetPanel != this.activePanel)
			{
				if (this.activePanel != null)
					this.activePanel.setVisible(false);
				
				this.activePanel = targetPanel;
				
				if (this.activePanel != null)
					this.activePanel.setVisible(true);
			}
		}
	},
	
	loginLogin : function(sender, userId, pass)
	{
		var target = this;
		this.service.login(userId, pass, function(result) { target.doLogin(result); });
	},
	
	doLogin : function(result)
	{
		if ((typeof result) == "number")
		{
			switch (result)
			{
				case -1 :
						alert("System error, please contact your administrator");
						break;
				case -2 :
						alert("User not found");
						break;
				case -3 :
						alert("Psssword mismatch");
						break;
			}
		}
		else
		{
			this.userObj = result;
			
			this.userInfo.setUserName(this.userObj.userName);
			
			this.login.setVisible(false);
			
			this.menu.setVisible(true);
			this.userInfo.setVisible(true);
			
			this.menu.setSelectedId("overview");
			this.menuSelect(this.menu, "overview");
			
			this.menuSelect(this.menu, "overview");
			
			// to do : multi project
			this.projectId = 1;
			
			this.overview.initPanel();
			this.ticket.initPanel();
			this.file.initPanel();
			this.people.initPanel();
		}
	},
	
	//-------------------- Event Handler --------------
	
	doScreenResized : function(newWidth, newHeight)
	{
		if (this.menu != null)
		{
			this.menu.setHeight(stage.getHeight());
			this.userInfo.setLeft(stage.getWidth() - this.userInfo.getWidth() - 10);
			
			this.login.setLeft((stage.getWidth() - this.login.getWidth()) / 2);
			this.login.setTop((stage.getHeight() - this.login.getHeight()) / 2);
			
			this.overview.setWidth(stage.getWidth() - this.overview.getLeft() - 10);
			this.overview.setHeight(stage.getHeight() - this.overview.getTop() - 10);
			
			this.milestone.setWidth(this.overview.getWidth());
			this.milestone.setHeight(this.overview.getHeight());
			
			this.ticket.setWidth(this.overview.getWidth());
			this.ticket.setHeight(this.overview.getHeight());
			
			this.file.setWidth(this.overview.getWidth());
			this.file.setHeight(this.overview.getHeight());
			
			this.people.setWidth(this.overview.getWidth());
			this.people.setHeight(this.overview.getHeight());
			
			this.setting.setWidth(this.overview.getWidth());
			this.setting.setHeight(this.overview.getHeight());
		}
	}
});

})();